package cn.edu.nhic.order;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SyncStateContract;
import android.widget.ListView;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.edu.nhic.order.adapter.ShopAdapter;
import cn.edu.nhic.order.bean.ShopBean;
import cn.edu.nhic.order.utils.Constant;
import cn.edu.nhic.order.utils.JsonParse;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 店铺列表
 */
public class ShopActivity extends AppCompatActivity {
    public static final int MSG_SHOP_OK = 2;  //获取数据
    private ListView shopListView;
    private List<ShopBean> shopBeanList;
    private MsgHandler msgHandler;
    private ShopAdapter shopAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        msgHandler=new MsgHandler();

        //1.初始化视图
        this.initView();

        //2.初始化数据
        this.initData1();

        //3.初始化适配器
        this.initAdapter();

        //4.初始化事件
        this.initEvent();
    }
    //1.初始化视图
    public void initView(){
        this.shopListView=this.findViewById(R.id.shop_listView);
    }
    //2.初始化数据
    public void initData(){
        this.shopBeanList=new ArrayList<ShopBean>();
        ShopBean sb1=new ShopBean();
        sb1.setShopName("柠檬可乐");
        sb1.setSaleNum(2476);
        sb1.setOfferPrice(new BigDecimal(20));
        sb1.setDistributionCost(new BigDecimal(2.5));
        sb1.setTime("配送15分钟");
        sb1.setFeature("南华大道回头率第2名");
        this.shopBeanList.add(sb1);
        ShopBean sb2=new ShopBean();
        sb2.setShopName("COCO都可");
        sb2.setSaleNum(2123);
        sb2.setOfferPrice(new BigDecimal(15));
        sb2.setDistributionCost(new BigDecimal(2));
        sb2.setTime("配送10分钟");
        sb2.setFeature("南华大道回头率第1名");
        this.shopBeanList.add(sb2);
        ShopBean sb3=new ShopBean();
        sb3.setShopName("维尔纳斯");
        sb3.setSaleNum(1234);
        sb3.setOfferPrice(new BigDecimal(21));
        sb3.setDistributionCost(new BigDecimal(3.5));
        sb3.setTime("配送8分钟");
        sb3.setFeature("南华大道回头率第3名");
        this.shopBeanList.add(sb3);
        ShopBean sb4=new ShopBean();
        sb4.setShopName("千层蛋糕");
        sb4.setSaleNum(2476);
        sb4.setOfferPrice(new BigDecimal(15));
        sb4.setDistributionCost(new BigDecimal(2));
        sb4.setTime("配送10分钟");
        sb4.setFeature("南华大道回头率第4名");
        this.shopBeanList.add(sb4);
    }
    //2.初始化数据
    public void initData1(){
        //创建http客户端
        OkHttpClient okHttpClient=new OkHttpClient();
        //构造网络请求对象request
        Request request=new Request.Builder().url(Constant.WEB_SITE+Constant.REQUEST_SHOP_URL).build();
        //呼叫call对象
        Call call=okHttpClient.newCall(request);
        //开启异步线程访问网络
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res=response.body().string();//获取店铺数据
                Message msg=new Message();
                msg.what=MSG_SHOP_OK;
                msg.obj=res;
                msgHandler.sendMessage(msg);
            }
        });
    }
    //3.初始化适配器
    public void initAdapter(){
        shopAdapter=new ShopAdapter(this,R.layout.shop_item);
        this.shopListView.setAdapter(shopAdapter);
    }
    //4.初始化事件
    public void initEvent(){

    }

    /**
     * 事件捕获
     * 消息助手分发消息
     */
    class MsgHandler extends Handler{
        @Override
        public void dispatchMessage(@NonNull Message msg) {
            super.dispatchMessage(msg);
            String shopListJson=(String)msg.obj;
            //解析获取JSON数据
            shopBeanList= JsonParse.getInstance().getShopList(shopListJson);
            shopAdapter.setData(shopBeanList);
//            switch (msg.what){
//                case MSG_SHOP_OK:
//                    if(msg.obj!=null){
//                        String shopListJson=(String)msg.obj;
//                        //解析获取JSON数据
//                        shopBeanList= JsonParse.getInstance().getShopList(shopListJson);
//
//                    }
//                    break;
//            }
        }
    }
}